<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-10-05
 * Time: 22:44
 */

namespace app\Http\Controllers\Wechat;


use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Models\MemberAccount;
use App\Models\MemberAccountLog;
use App\Models\Order;
use App\Models\WithdrawLog;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class WithdrawController extends Controller
{
    public function getIndex()
    {
        $memberAccount = MemberAccount::query()
            ->where('openid', Session::get('wechat_openid'))
            ->first();

        $member = Member::getByOpenid(Session::get('wechat_openid'));

        return view('wechat.withdraw.index', [
            'memberAccount' => $memberAccount,
            'openid' => Session::get('wechat_openid'),
            'member' => $member,
        ]);
    }

    public function postIndex(Request $request)
    {
        $this->validate($request, [
            'openid' => 'required',
            'fee' => 'required',
            'alipay_account' => 'required',
        ]);
        if ($request->openid != Session::get('wechat_openid')) {
            abort(404);
        }
        // 开始提现
        DB::beginTransaction();
        try {
            $memberAccount = MemberAccount::getMemberAccountByOpenid(Session::get('wechat_openid'));
            if ($request->fee - $memberAccount->balance >= 0.01) {
                throw new \Exception('申请失败，账户余额不足');
            }
            $memberAccount->balance -= $request->fee;
            $memberAccount->lock_balance += $request->fee;
            $memberAccount->save();
            $withdrawLog = new WithdrawLog();
            $withdrawLog->openid = Session::get('wechat_openid');
            $withdrawLog->status = 1;
            $withdrawLog->amount = $request->fee;
            $withdrawLog->save();
            $member = Member::getByOpenid(Session::get('wechat_openid'));
            $member->alipay_account = $request->alipay_account;
            $member->alipay_name = $request->alipay_name;
            $member->save();
            Session::put('message', '申请成功，稍后请关注支付宝信息<br/>此次提现将扣除手续费￥' . $request->fee * fee_rate() / 100);
            DB::commit();
        } catch (\Exception $exception) {
            Session::put('message', $exception->getMessage());
            DB::rollback();
        }
        return redirect('/wechat/withdraw/tip');
    }

    public function getTip()
    {
        return view('wechat.withdraw.tip', ['message' => Session::pull('message')]);
    }

    public function getList()
    {
        $withdrawLogs = WithdrawLog::query()
            ->where('openid', Session::get('wechat_openid'))
            ->orderBy('id', 'desc')
            ->take(20)
            ->get();
        return view('wechat.withdraw.list', compact('withdrawLogs'));
    }

    public function getRecord()
    {
        $memberAccountLogs = MemberAccountLog::query()
            ->where('openid', Session::get('wechat_openid'))
            ->where('type', 2)
            ->with('wechat_upload')
            ->orderBy('id', 'desc')
            ->take(100)
            ->get();
        return view('wechat.withdraw.record', compact('memberAccountLogs'));
    }
}